//var cpuRatio,memoryRatio,threadRatio,flowCountDayRatio,flowCountHourRatio;

var currentNodeId = "",currnetNodeType = "";
$(function() {
	Highcharts.setOptions({
        global: {
            useUTC: false
        }
    });
	
//	currentNodeId = "nodeVm6_7minimal";
//	currnetNodeType = "esb";
//	refleshChartsData("nodeVm6_7minimal");
	if(result.esb_nodeCount>0){
		var esb_records = result.esb_records;
		for(var i=0;i<esb_records.length;i++){
			refleshChartsData(esb_records[i].nodeId);
		}
	}
	
	if(SHOW_MENU_IFS && result.ifs_nodeCount>0){
		var ifs_records = result.ifs_records;
		for(var i=0;i<ifs_records.length;i++){
			refleshChartsData(ifs_records[i].nodeId);
		}
	}
});

function refleshChartsData(nodeId){
	
	Ajax({
		url: contextPath+"/console/monitor/queryChartsData",
		isMask : true,
		data: {
			nodeId: nodeId
		},
		success: function (data) {
			setChartsData(data,nodeId);
		}
	});
	
}

function setChartsData(result,nodeId){
	
	if(result.timeAry!=undefined && 
			result.cpuAry!=undefined &&
			result.memoryAry!=undefined &&
			result.threadAry!=undefined){
		
		var timeAry = result.timeAry;
		var cpuAry = result.cpuAry;
		var memoryAry = result.memoryAry;
		var threadAry = result.threadAry;
		
		var cpuChart,memoryChart,threadChart;
		var cpuRatioData = [],memoryRatioData = [],threadRatioData = []; 
		for(var i=0;i<timeAry.length;i++){
			cpuRatioData.push([
								timeAry[i],
								cpuAry[i].data
	                        ]);
			memoryRatioData.push([
			                   timeAry[i],
			                   memoryAry[i].data
			                   ]);
			threadRatioData.push([
			                   timeAry[i],
			                   threadAry[i].data
			                   ]);
		}
//		var cpuRatio = initCpuChartObject();
//		var memoryRatio = initMemoryChartObject();
//		var threadRatio = initThreadChartObject();
		
		//cpu使用率
		//cpuRatio.series[0].data=cpuRatioData;
		cpuChart = new Highcharts.Chart({
		        	credits:{
					     enabled:false // 禁用版权信息
					},
		       chart: {
		           type: 'spline',
		           renderTo:'container_'+nodeId+'_1',
		           animation: Highcharts.svg, // don't animate in old IE
		           marginRight: 10,
		           events: {
		               load: function () {
		               var series = this.series[0];
		                   setInterval(function () {
		                   	Ajax({
		                    		url: contextPath+"/console/monitor/queryLastChartsData",
		                    		isMask : false,
		                    		data: {
		                    			nodeId: nodeId,
		                    			type: "cpuAry"
		                    		},
		                    		success: function (result) {
		                    			
		                    			if(series.data!=undefined && series.data.length>=24){
		                    				series.data[0].remove();
		                    			}
		                    			var ary = new Array();
		                    			ary[0]= result.key;
		                    			ary[1]= result.value;
		                    			series.addPoint(ary);
		                    		}
		                    	});
		                    }, 5000);
		               }
		           }
		       },
		       title: {
		           text: 'CPU使用率'
		       },
		       xAxis: {
		           type: 'datetime',
		           tickPixelInterval: 150
		       },
		       yAxis: {
		           title: {
		               text: '使用百分比 (%)'
		           },
		           min:0,
		           max:100,
		           plotLines: [{
		               value: 0,
		               width: 1,
		               color: '#808080'
		           }]
		       },
		       tooltip: {
		           formatter: function () {
		               return '<b>' + this.series.name + '</b><br/>' +
		                   Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
		                   'CPU使用率(%)：'+Highcharts.numberFormat(this.y, 2);
		           }
		       },
		       legend: {
		           enabled: false
		       },
		       exporting: {
		           enabled: false
		       },
		       series: [{
		           name: 'CPU使用率',
		           data:cpuRatioData
		       }]
		});
		//内存使用
		//memoryRatio.series[0].data=memoryRatioData;
		memoryChart = new Highcharts.Chart({
				credits:{
					enabled:false // 禁用版权信息
				},
				chart: {
					type: 'spline',
					renderTo:'container_'+nodeId+'_2',
					animation: Highcharts.svg, // don't animate in old IE
					marginRight: 10,
					events: {
						load: function () {
							var series = this.series[0];
							setInterval(function () {
								Ajax({
									url: contextPath+"/console/monitor/queryLastChartsData",
									isMask : false,
									data: {
										nodeId: nodeId,
										type: "memoryAry"
									},
									success: function (result) {
										
										if(series.data!=undefined && series.data.length>=24){
											series.data[0].remove();
										}
										var ary = new Array();
										ary[0]= result.key;
										ary[1]= result.value;
										series.addPoint(ary);
									}
								});
							}, 5000);
						}
					}
				},
				title: {
					text: '内存使用'
				},
				xAxis: {
					type: 'datetime',
					tickPixelInterval: 150
				},
				yAxis: {
					title: {
						text: '使用内存数(M)'
					},
					min:0,
	//				max:10,
					plotLines: [{
						value: 0,
						width: 1,
						color: '#808080'
					}]
				},
				tooltip: {
					formatter: function () {
						return '<b>' + this.series.name + '</b><br/>' +
						Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
						'内存使用'+ this.y +'M';
					}
				},
				legend: {
					enabled: false
				},
				exporting: {
					enabled: false
				},
				series: [{
					name: '内存使用',
					data:memoryRatioData
				}]
		});
		//线程数
		//threadRatio.series[0].data=threadRatioData;
		threadChart = new Highcharts.Chart({
			credits:{
				enabled:false // 禁用版权信息
			},
			chart: {
				type: 'spline',
				renderTo:'container_'+nodeId+'_3',
				animation: Highcharts.svg, // don't animate in old IE
				marginRight: 10,
				events: {
					load: function () {
						var series = this.series[0];
						setInterval(function () {
							Ajax({
								url: contextPath+"/console/monitor/queryLastChartsData",
								isMask : false,
								data: {
									nodeId: nodeId,
									type: "threadAry"
								},
								success: function (result) {
									
									if(series.data!=undefined && series.data.length>=24){
										series.data[0].remove();
									}
									var ary = new Array();
									ary[0]= result.key;
									ary[1]= result.value;
									series.addPoint(ary);
								}
							});
						}, 5000);
					}
				}
			},
			title: {
				text: '线程数'
			},
			xAxis: {
				type: 'datetime',
				tickPixelInterval: 150
			},
			yAxis: {
				title: {
					text: '占用线程数(个)'
				},
				min:0,
				plotLines: [{
					value: 0,
					width: 1,
					color: '#808080'
				}]
			},
			tooltip: {
				formatter: function () {
					return '<b>' + this.series.name + '</b><br/>' +
					Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
					'线程占用'+ this.y +'个';
				}
			},
			legend: {
				enabled: false
			},
			exporting: {
				enabled: false
			},
			series: [{
				name: '线程数',
				data:threadRatioData
			}]
	});
	
	}
}
